package com.qsw.radioview.view;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * 选项改变时重绘所有子RadioButton的RadioGroup
 */
public class RadioView extends RadioGroup {
    private OnCheckedChangeListener mUserOnCheckedChangeListener;

    public RadioView(Context context) {
        super(context);
        init();
    }

    public RadioView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setOrientation(HORIZONTAL);
        super.setOnCheckedChangeListener(mOnCheckedChangeListener);
    }

    @Override
    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        mUserOnCheckedChangeListener = listener;
    }

    /**
     * 设置所有子RadioViewButton绘制类型
     *
     * @param drawStroke 是否绘制边框
     * @param drawFill   是否绘制填充
     */
    public void setAllRVBDrawStyle(boolean drawStroke, boolean drawFill) {
        for (int i = 0; i < getChildCount(); i++) {
            if (getChildAt(i) instanceof RadioViewButton) {
                ((RadioViewButton) getChildAt(i)).setDrawStyle(drawStroke, drawFill);
            }
        }
    }

    /**
     * 设置所有子RadioViewButton边框线宽
     *
     * @param strokeLineWidth 边框线宽，单位：px
     */
    public void setAllRVBStrokeLineWidth(int strokeLineWidth) {
        for (int i = 0; i < getChildCount(); i++) {
            if (getChildAt(i) instanceof RadioViewButton) {
                ((RadioViewButton) getChildAt(i)).setStrokeLineWidth(strokeLineWidth);
            }
        }
    }

    /**
     * 设置所有子RadioViewButton选中时的边框颜色
     *
     * @param checkedStrokeColor 选中时的边框颜色
     */
    public void setAllRVBCheckedStrokeColor(int checkedStrokeColor) {
        for (int i = 0; i < getChildCount(); i++) {
            if (getChildAt(i) instanceof RadioViewButton) {
                ((RadioViewButton) getChildAt(i)).setCheckedStrokeColor(checkedStrokeColor);
            }
        }
    }

    /**
     * 设置所有子RadioViewButton未选中时的边框颜色
     *
     * @param noCheckedStrokeColor 未选中时的边框颜色
     */
    public void setAllRVBNoCheckedStrokeColor(int noCheckedStrokeColor) {
        for (int i = 0; i < getChildCount(); i++) {
            if (getChildAt(i) instanceof RadioViewButton) {
                ((RadioViewButton) getChildAt(i)).setNoCheckedStrokeColor(noCheckedStrokeColor);
            }
        }
    }

    /**
     * 设置所有子RadioViewButton选中时的填充色
     *
     * @param checkedFillColor 选中时的填充色
     */
    public void setAllRVBCheckedFillColor(int checkedFillColor) {
        for (int i = 0; i < getChildCount(); i++) {
            if (getChildAt(i) instanceof RadioViewButton) {
                ((RadioViewButton) getChildAt(i)).setCheckedFillColor(checkedFillColor);
            }
        }
    }

    /**
     * 设置所有子RadioViewButton未选中时的填充色
     *
     * @param noCheckedFillColor 未选中时的填充色
     */
    public void setAllRVBNoCheckedFillColor(int noCheckedFillColor) {
        for (int i = 0; i < getChildCount(); i++) {
            if (getChildAt(i) instanceof RadioViewButton) {
                ((RadioViewButton) getChildAt(i)).setNoCheckedFillColor(noCheckedFillColor);
            }
        }
    }

    /**
     * 设置所有子RadioViewButton选中时的文字颜色
     *
     * @param checkedTextColor 选中时的文字颜色
     */
    public void setAllRVBCheckedTextColor(int checkedTextColor) {
        for (int i = 0; i < getChildCount(); i++) {
            if (getChildAt(i) instanceof RadioViewButton) {
                ((RadioViewButton) getChildAt(i)).setCheckedTextColor(checkedTextColor);
            }
        }
    }

    /**
     * 设置所有子RadioViewButton未选中时的文字颜色
     *
     * @param noCheckedTextColor 未选中时的文字颜色
     */
    public void setAllRVBNoCheckedTextColor(int noCheckedTextColor) {
        for (int i = 0; i < getChildCount(); i++) {
            if (getChildAt(i) instanceof RadioViewButton) {
                ((RadioViewButton) getChildAt(i)).setNoCheckedTextColor(noCheckedTextColor);
            }
        }
    }

    private OnCheckedChangeListener mOnCheckedChangeListener = new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            for (int i = 0; i < group.getChildCount(); i++) {
                if (group.getChildAt(i) instanceof RadioButton) {
                    group.getChildAt(i).invalidate();
                }
            }
            if (mUserOnCheckedChangeListener != null) {
                mUserOnCheckedChangeListener.onCheckedChanged(group, checkedId);
            }
        }
    };
}
